package com.flot.yazb.home.moudle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BFragment;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.fate.FateModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.home.setting.UserDetailActivity;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.ToastUtil;
import com.flot.yazb.widget.cardSwipLayout.CardConfig;
import com.flot.yazb.widget.cardSwipLayout.CardItemTouchHelperCallback;
import com.flot.yazb.widget.cardSwipLayout.CardLayoutManager;
import com.flot.yazb.widget.cardSwipLayout.CardRecyclerView;
import com.flot.yazb.widget.cardSwipLayout.OnSwipeListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class FateFragment extends BFragment {

    @BindView(R.id.mrv)
    CardRecyclerView mRecycleview;
    @BindView(R.id.next)
    ImageView next;
    @BindView(R.id.like)
    ImageView like;
    private List<FateModel> dataList = new ArrayList<>();
    private BRAdapter<FateModel> rvAdapter;
    private View view;
    private UserModel argsModel;
    private MyAdapter cardAdapter;
    private int dataSize = 0;
    @Override
    protected View initRootView(LayoutInflater inflater) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_home_fate, null);
        }
        ButterKnife.bind(this, view);
        ViewGroup myparent = (ViewGroup) view.getParent();
        if (getArguments() != null) {
            argsModel = (UserModel) getArguments().getSerializable("args");
        }
        if (myparent != null) {
            myparent.removeView(view);
        }
        return view;
    }
    public static FateFragment newInstance(UserModel model) {
        FateFragment fragment = new FateFragment();
        Bundle args = new Bundle();
        args.putSerializable("args", model);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        getFateData();

        cardAdapter = new MyAdapter();
        mRecycleview.setItemAnimator(new DefaultItemAnimator());
        mRecycleview.setAdapter(cardAdapter);
        final CardItemTouchHelperCallback cardCallback = new CardItemTouchHelperCallback(mRecycleview,mRecycleview.getAdapter(), dataList);
        cardCallback.setOnSwipedListener(new OnSwipeListener<FateModel>() {

            @Override
            public void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {
                MyAdapter.MyViewHolder myHolder = (MyAdapter.MyViewHolder) viewHolder;
                viewHolder.itemView.setAlpha(1 - Math.abs(ratio) * 0.2f);
                if (direction == CardConfig.SWIPING_LEFT) {
                    myHolder.dislikeImageView.setAlpha(Math.abs(ratio));
                } else if (direction == CardConfig.SWIPING_RIGHT) {
                    myHolder.likeImageView.setAlpha(Math.abs(ratio));
                } else {
                    myHolder.dislikeImageView.setAlpha(0f);
                    myHolder.likeImageView.setAlpha(0f);
                }
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, FateModel o, int direction) {
                MyAdapter.MyViewHolder myHolder = (MyAdapter.MyViewHolder) viewHolder;
                viewHolder.itemView.setAlpha(1f);
                myHolder.dislikeImageView.setAlpha(0f);
                myHolder.likeImageView.setAlpha(0f);
                like.setBackgroundResource(R.mipmap.icon_jiaoyou_xin_gray);
                dataSize--;
                cardAdapter.notifyDataSetChanged();
            }

            @Override
            public void onSwipedClear() {
                mRecycleview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getFateData();
                    }
                }, 500);
            }

        });
        final ItemTouchHelper touchHelper = new ItemTouchHelper(cardCallback);
        final CardLayoutManager cardLayoutManager = new CardLayoutManager(mRecycleview, touchHelper);
        mRecycleview.setLayoutManager(cardLayoutManager);
        touchHelper.attachToRecyclerView(mRecycleview);
        cardAdapter.notifyDataSetChanged();

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like.setBackgroundResource(R.mipmap.icon_jiaoyou_xin_gray);
                cardCallback.handleCardSwipe(CardConfig.SWIPING_LEFT, 300L);
                if (dataList.size() == 0) {
                    mRecycleview.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            getFateData();
                        }
                    }, 500);
                }
            }
        });
        like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like.setBackgroundResource(R.mipmap.icon_jiaoyou_xin_red);
                cardCallback.handleCardSwipe(CardConfig.SWIPING_RIGHT, 300L);
            }
        });
    }


    /**
     * 获取缘分列表数据
     */
    private void getFateData() {
        showProgress();
        dataSize = 0;
        like.setBackgroundResource(R.mipmap.icon_jiaoyou_xin_gray);
        FateModel.getFateList(new BModel.Transit<FateModel[]>(getActivity()) {
            @Override
            public void onBorn(FateModel[] serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    dataList.clear();
                    dataList.addAll(Arrays.asList(serializable));
                    dataSize = dataList.size();
                    if (cardAdapter != null)
                        cardAdapter.notifyDataSetChanged();
                    if (dataList.size() == 0) {
                        getFateData();
                    }
                } else {
                    ToastUtil.showToast(getActivity(), message);
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ImageView avatarImageView = ((MyViewHolder) holder).avatarImageView;
            GlideImageLoaderUtil.loadImage(avatarImageView, dataList.get(position).thumb, R.mipmap.activity_splash_background);
            ((MyViewHolder) holder).tvname.setText(dataList.get(position).name);
            ((MyViewHolder) holder).tvage.setText("♀ " + dataList.get(position).age + "岁");
            ((MyViewHolder) holder).address.setText(dataList.get(position).province);
            ((MyViewHolder) holder).tvage.setText("♀ " + dataList.get(position).age + "岁");
            ((MyViewHolder) holder).num.setText(dataSize + "张");
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            ImageView avatarImageView, dislikeImageView, likeImageView;
            TextView tvname, tvage, address, num;
            LinearLayout linout;

            MyViewHolder(View itemView) {
                super(itemView);
                avatarImageView = (ImageView) itemView.findViewById(R.id.iv_avatar);
                tvname = (TextView) itemView.findViewById(R.id.tv_name);
                tvage = (TextView) itemView.findViewById(R.id.tv_age);
                address = (TextView) itemView.findViewById(R.id.address);
                linout = (LinearLayout) itemView.findViewById(R.id.root);
                num = (TextView) itemView.findViewById(R.id.num);
                likeImageView = (ImageView) itemView.findViewById(R.id.iv_like);
                dislikeImageView = (ImageView) itemView.findViewById(R.id.iv_dislike);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
                        intent.putExtra("mid", dataList.get(getAdapterPosition()));
                        startActivity(intent);
                    }
                });
            }

        }
    }

}
